import {Component, OnDestroy, OnInit, ViewEncapsulation} from '@angular/core';
import {SaleService} from "@sale/sale.service";
import {ActivatedRoute, Params, Router} from "@angular/router";

import {Subject} from "rxjs/Subject";
import {AuthService} from "@shared/service/auth/auth.service";
import {TrainSaleSignFormRespVo} from "@interface/index";

@Component({
  selector: 'seed-add-trial-course',
  templateUrl: './add-trial-course.component.html',
  styleUrls: ['./add-trial-course.component.scss', '../add-trial-campus/add-trial-campus.component.scss'],
  encapsulation: ViewEncapsulation.None,
})
export class AddTrialCourseComponent implements OnInit, OnDestroy {
  public orgUserId: string;
  public courseInfo: Array<TrainSaleSignFormRespVo> = [];
  public studentId: string;
  public saleUserId: string;
  public ngUnsubscribe: Subject<void> = new Subject<void>();

  constructor(
    public saleService: SaleService,
    public route: ActivatedRoute,
    public router: Router,
    private authService: AuthService
  ) {
    this.orgUserId = this.authService.getAuthOrgId();
    this.route.params.subscribe((param: Params) => {
      this.studentId = param['studentId'];
      this.saleUserId = param['saleUserId'];
      this.saleService.getCourseList(this.orgUserId, 2).takeUntil(this.ngUnsubscribe).subscribe(res => {
        if (res.result === '0') {
          const data = res.data;
          for (let i = 0; i < data.length; i++) {
            if (data[i].existenceLesson) {
              this.courseInfo.push(data[i]);
            }
          }
        }
      });
    });
  }

  goToCampus(courseId) {
    this.router.navigate([`/sale/campus/${this.studentId}/${this.saleUserId}/${courseId}`]);
  }

  ngOnInit() {
  }

  ngOnDestroy() {
    this.ngUnsubscribe.next();
    this.ngUnsubscribe.complete();
  }

}
